package com.example.lending.View;


import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.Toolbar;

import com.example.lending.Model.Book;
import com.example.lending.Model.User;
import com.example.lending.R;
import com.example.lending.View.Adapter.TypeAdapter;
import com.example.lending.ViewModel.BookViewModel;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class UpdataBookFragment extends Fragment {
    EditText Name,Number,Count,Type,Pub,Intro;
    Toolbar toolbar;
    Button button;
    BookViewModel bookViewModel;
    int number=-1;
    public UpdataBookFragment() {
        // Required empty public constructor
    }
    public void inteView(){
        Name=getView().findViewById(R.id.et_updatabook_name);
        Number=getView().findViewById(R.id.et_updatabook_number);
        Count=getView().findViewById(R.id.et_updatabook_count);
        Type=getView().findViewById(R.id.et_updatabook_type);
        Pub=getView().findViewById(R.id.et_updatabook_pub);
        Intro=getView().findViewById(R.id.et_updatabook_intro);
        button=getView().findViewById(R.id.btn_book_update);
        bookViewModel= ViewModelProviders.of(getActivity()).get(BookViewModel.class);
    }

    public Book getBook(String name ,int number,String count,String type,String pub,String intro){
        Book book=new Book();
        book.setType(type);
        book.setName(name);
        book.setCount(count);
        book.setIntroduce(intro);
        book.setCount(count);
        book.setPublic_com(pub);
        return book;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        inteView();

        final List<Book>books=bookViewModel.getAllBooksLive().getValue();

            //将更新的书籍的名字导入到这里来
            final String getname = getActivity().getSharedPreferences("adapter", Context.MODE_PRIVATE).getString("bookname", "");

            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(final View v) {
                    final String name=Name.getText().toString();
                    if(!Number.getText().toString().isEmpty())
                        number=Integer.parseInt(Number.getText().toString());
                    final String count=Count.getText().toString();
                    final String type=Type.getText().toString();
                    final String pub=Pub.getText().toString();
                    final String intro=Intro.getText().toString();
                    if(TextUtils.isEmpty(count)||TextUtils.isEmpty(type)||TextUtils.isEmpty(pub)||TextUtils.isEmpty(name)||TextUtils.isEmpty(intro)||(number==-1)){
                        Toast.makeText(getActivity(), "请将数据补充完毕", Toast.LENGTH_SHORT).show();
                    }else {
                        LiveData<List<Book>> listLiveData = bookViewModel.findBookWithPattern(getname);
                        listLiveData.observe(getViewLifecycleOwner(), new Observer<List<Book>>() {
                            @Override
                            public void onChanged(List<Book> books) {
                                //for (int i = 0; i <= books.size(); i++) {
                                    //bookViewModel.deleteBooks(books.get(i));
                                    //bookViewModel.insertBooks(getBook(name, number, count, type, pub, intro));
                                int i=0;
                                    books.get(i).setType(type);
                                    books.get(i).setName(name);
                                    books.get(i).setCount(count);
                                    books.get(i).setIntroduce(intro);
                                    books.get(i).setCount(count);
                                    books.get(i).setPublic_com(pub);
                                    bookViewModel.updataBooks(books.get(i));
                                    Toast.makeText(getActivity(), "修改成功", Toast.LENGTH_SHORT).show();

                                //}
                                //Toast.makeText(getActivity(), "更新失败", Toast.LENGTH_SHORT).show();
                                NavController controller = Navigation.findNavController(v);
                                controller.navigate(R.id.action_updataBookFragment2_to_homeFragment);
                            }
                        });
                    }
                }
            });

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_updata_book, container, false);
    }

}
